Ext.Loader.setPath('Ext.ux', '../ux/');
Ext.require(['*']);
Ext.onReady(function() {
	
	var centerPanel = new Ext.panel.Panel({
		region: 'center',
		layout: 'fit',
		items: [],
		margin: '0 0 0 2',
		border: false
	});
	
	Ext.define("menu", {
	    extend: "Ext.data.Model",
	    fields: ['path', 'text']
	});
	
	var treeStore = Ext.create('Ext.data.TreeStore', {
		model:'menu',
		proxy: {
			type: 'ajax',
			url: '/userInfo.action?type=menu'
		},
		listeners: {   
            load: function(thiz, node, records, successful, eOpts) {
				var url = records[0].get('path');
				centerPanel.update(Ext.String.format('<iframe style="position:absolute; height: 100%; width: 100%" frameborder="0" src="{0}"></iframe>', url));
			}
        },
		autoLoad: true
	});
	
	
	
	var tree = Ext.create('Ext.tree.Panel', {
		store: treeStore,
		region: 'west',
		rootVisible: false,
		title: '导航栏',
		split: true, // 拖动
		width: 200,
		height:800
	});
	tree.on('itemclick', function(sender, record, item, index, e) {
		var url = record.get('path');
		centerPanel.update(Ext.String.format('<iframe style="position:absolute; height: 100%; width: 100%" frameborder="0" src="{0}"></iframe>', url));
				
	});
	var system = Ext.create('Ext.toolbar.TextItem', {text: "    <B>电子报刊后台管理系统</B>"});
	var wellcome = Ext.create('Ext.toolbar.TextItem', {text: "    欢迎 , &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;!"});
	var clock = Ext.create('Ext.toolbar.TextItem', {text: "当前时间  : "+Ext.Date.format(new Date(), 'Y-m-d g:i:s A')});
	//工具条
	var tb = new Ext.Toolbar({
		items:[system,wellcome,Ext.create('Ext.toolbar.Fill'),clock], //工具条中加入button
		listeners: {
            render: {
                fn: function(){
                    //Ext.fly(clock.getEl().parent()).addCls('x-status-text-panel').createChild({cls:'spacer'});

                 Ext.TaskManager.start({
                     run: function(){
                         Ext.fly(clock.getEl()).update("当前时间  : "+Ext.Date.format(new Date(), 'Y-m-d g:i:s A'));
                     },
                     interval: 1000
                 });
                },
                delay: 100
            }
        }
	});

	
	var infoPanel = new Ext.panel.Panel({
		
		region: 'center',
		layout: 'border',
		margin: '0 0 0 2',
		//border: true,
		tbar:tb,
		items:[tree,centerPanel],
		

		height:480
	});
	
	
    var view = new Ext.container.Viewport({
		layout: 'border',
		renderTo: Ext.getBody(),
		items: [
				infoPanel
				//,
				//tree, 
		        //centerPanel
		        
		]
	});
	
	var getUserInfo = function() {
		var url = "/userInfo.action?type=info";
		Ext.Ajax.request({
            url: url,
            scope: this,  
            success: function(response, options) {
            	var ret = Ext.JSON.decode(response.responseText); 
            	var wcs = "    欢迎 , <B>" + ret.showname +"</B> !   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            	var domains = ret.domains;
            		Ext.fly(wellcome.getEl()).update(wcs);
            	if(domains.length >0){
            		Ext.fly(dd.getEl()).update("Domain : &nbsp;<B>" + domains[0].name +"</B>");
            	}
            	tb.doLayout(false, true); 
            	//infoPanel.update(response.responseText);
            },
            failure: function(response, options) {
	        	
			}
	    });
    };
    getUserInfo();
});